/*
 * ReportPrinter.java
 *
 * Created on 12 de Mar�o de 2003, 22:44
 */
package saci.reptil;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PageFormat;
import java.awt.Component;
import java.util.Vector;

/**
 * Classe que representa um componente imprim�vel
 *
 * @author  saci
 */
public class ReportPrinter implements Printable {

    private Vector design;
    private Component[] componentsInReport;
    private ReportData reportData;
    private int row;
    private int actualRow;
    private ReportManager reportManager;

    /**
     * Cria uma nova inst�ncia da classe
     * 
     * @param rm o gerenciador do relatorio
     * @param v o desenho do relat�rio
     * @param c os componentes fora das bandas
     */
    public ReportPrinter(ReportManager rm, Vector v, Component[] c) {
        this.design = v;
        this.componentsInReport = c;
        this.reportManager = rm;
    }

    /**
     * Cria uma nova inst�ncia da classe
     * 
     * @param rm o gerenciador do relatorio
     * @param v o desenho do relat�rio
     * @param c os componentes fora das bandas
     * @param r a fonte de dados do relat�rio
     * @param row a linha inicial dos dados
     */
    public ReportPrinter(ReportManager rm, Vector v, Component[] c, ReportData r, int row) {
        this.design = v;
        this.componentsInReport = c;
        this.reportData = r;
        this.row = row;
        this.reportManager = rm;
    }

    /**
     * Imprimi a p�gina
     */
    public int print(Graphics g, PageFormat format, int page) throws PrinterException {
        actualRow = row;

        Graphics2D g2 = (Graphics2D) g;
        Graphics temp;

        g2.translate(format.getImageableX(), format.getImageableY());

        System.out.println("Printing... " + format.getImageableWidth() + "x" + format.getImageableHeight());

        int pageHeight, leftInPage;

        leftInPage = new Double(Math.floor(format.getImageableHeight())).intValue();
        pageHeight = leftInPage;

        if (design != null) {
            Band b;

            for (int i = 0; i < design.size(); i++) {
                b = (Band) design.get(i);

                b.prepareAll(page, new Object[]{new Integer(actualRow), reportData, reportManager});
                if (b instanceof DetailBand) {
                    actualRow++;
                }

                if (b instanceof FooterBand) {
                    b.setLocation(0, pageHeight - b.getHeight());
                    temp = g2.create(0, pageHeight - b.getHeight(), b.getWidth(), b.getHeight());
                } else {
                    b.setLocation(0, pageHeight - leftInPage);
                    temp = g2.create(0, pageHeight - leftInPage, b.getWidth(), b.getHeight());
                }

                if (b.getHeight() > 0) {
                    leftInPage -= b.getHeight();
                    b.paint(temp);
                }
            }
        }

        if (componentsInReport != null) {
            for (int i = 0; i < componentsInReport.length; i++) {
                componentsInReport[i].paint(g.create(componentsInReport[i].getLocation().x, componentsInReport[i].getLocation().y, componentsInReport[i].getWidth(), componentsInReport[i].getHeight()));
            }
        }

        return Printable.PAGE_EXISTS;
    }

    /**
     * Altera o desenho da p�gina
     */
    public void setPage(Vector v) {
        this.design = v;
    }

    /**
     * Retorna o desenho da p�gina
     */
    public Vector getPage() {
        return design;
    }
}
